package com.huifaapp.mall.activity.Homefragment.ShengqianActvity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.huifaapp.mall.R;
import com.huifaapp.mall.afinal.SPConstants;
import com.huifaapp.mall.api.ApiManager;
import com.huifaapp.mall.bean.Huishengqian.HBshare;
import com.huifaapp.mall.uitils.AUniqueIdentifier;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

/*
炫一下
*/
public class Xuan_Yi_XiaActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private ImageView imageView;
    private  ImageView imaback;
    private  PopupWindow window;
    private HBshare.DataBean data;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_xuan__yi__xia);
    button = (Button) findViewById(R.id.but_shareh);
    button.setOnClickListener(this);
    imageView = (ImageView) findViewById(R.id.imager);
    imaback = (ImageView) findViewById(R.id.Gvmine_back);
    imaback.setOnClickListener(this);
                getNetHaibao();
       }

    private void getNetHaibao() {

         String url = ApiManager.shreHB+ "?token=" + SPUtils.getInstance().getString(SPConstants.KEY_USER_TOKEN) + "&appId=" + AUniqueIdentifier.getDeviceId(this);
        Log.e("shaishouyi", "主题url====" + url);
        HttpUtils http = new HttpUtils();
        http.send(HttpRequest.HttpMethod.POST, url, new RequestCallBack<String>() {

            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e(123+"",responseInfo.result);
                HBshare hBshare = new Gson().fromJson(responseInfo.result, HBshare.class);
                if (hBshare.getCode().equals("10000")){
                    data = hBshare.getData();
                    String image = data.getImage();

                    Glide.with(Xuan_Yi_XiaActivity.this).load(image).into(imageView);



                }
                else {
                    Toast.makeText(Xuan_Yi_XiaActivity.this,hBshare.getMsg(),Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(Xuan_Yi_XiaActivity.this,s,Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    public void onClick(View v) {
            switch (v.getId()){
                case R.id.but_shareh:
                    //分享
                    View view1 =   LayoutInflater.from(Xuan_Yi_XiaActivity.this).inflate(R.layout.popwindownshangtan,null, false);
                    window = new PopupWindow(view1, RelativeLayout.LayoutParams.MATCH_PARENT,600,true );
                    window.setOutsideTouchable(true);
                    window.setTouchable(true);
                    WindowManager.LayoutParams lp = getWindow().getAttributes();
                    lp.alpha = 0.7f;
                    getWindow().setAttributes(lp);
                    window.setOnDismissListener(new PopupWindow.OnDismissListener() {
                        @Override
                        public void onDismiss() {
                            WindowManager.LayoutParams lp = getWindow().getAttributes();
                            lp.alpha = 1f;
                            getWindow().setAttributes(lp);
                        }
                    });
                /*
                        分享朋友
                 */
                    view1.findViewById(R.id.share_penyou).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            UMImage image = new UMImage(Xuan_Yi_XiaActivity.this, data.getImage());//网络图片
                            image.compressStyle = UMImage.CompressStyle.SCALE;//大小压缩，默认为大小压缩，适合普通很大的图
                            image.compressStyle = UMImage.CompressStyle.QUALITY;//质量压缩，适合长图的分享
                            new ShareAction(Xuan_Yi_XiaActivity.this).setPlatform(SHARE_MEDIA.WEIXIN).withMedia(image).setCallback(new UMShareListener() {
                                @Override
                                public void onStart(SHARE_MEDIA share_media) {

                                }

                                @Override
                                public void onResult(SHARE_MEDIA share_media) {
                                    window.dismiss();
                                    Toast.makeText(Xuan_Yi_XiaActivity.this, " 分享成功", Toast.LENGTH_SHORT).show();

                                }

                                @Override
                                public void onError(SHARE_MEDIA share_media, Throwable throwable) {

                                }

                                @Override
                                public void onCancel(SHARE_MEDIA share_media) {

                                }
                            }).share();



                        }
                    });
                /*
                        分享朋友圈
                 */
                    view1.findViewById(R.id.share_pengyouquna).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            UMImage image = new UMImage(Xuan_Yi_XiaActivity.this, data.getImage());//网络图片
                            image.compressStyle = UMImage.CompressStyle.SCALE;//大小压缩，默认为大小压缩，适合普通很大的图
                            image.compressStyle = UMImage.CompressStyle.QUALITY;//质量压缩，适合长图的分享
                            new ShareAction(Xuan_Yi_XiaActivity.this).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE).withMedia(image).setCallback(new UMShareListener() {
                                @Override
                                public void onStart(SHARE_MEDIA share_media) {

                                }

                                @Override
                                public void onResult(SHARE_MEDIA share_media) {
                                    window.dismiss();
                                    Toast.makeText(Xuan_Yi_XiaActivity.this, " 分享成功", Toast.LENGTH_SHORT).show();

                                }

                                @Override
                                public void onError(SHARE_MEDIA share_media, Throwable throwable) {

                                }

                                @Override
                                public void onCancel(SHARE_MEDIA share_media) {

                                }
                            }).share();

                        }
                    });
                    //关闭弹窗
                    view1.findViewById(R.id.imgguanbi).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            window.dismiss();
                        }
                    });
                    window.showAtLocation(getWindow().getDecorView(), Gravity.BOTTOM , 0, 0);

                    break;

                case  R.id.Gvmine_back:
                    finish();
                    break;

            }

    }
}
